<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AdminPermission;
use App\Models\AdminRole;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class RoleController extends Controller
{
    public function indexs()
    {
        $roles = AdminRole::paginate(10);
        return view('role.index',compact('roles'));
    }

    /**
     * 角色视图
     */
    public function role(){
        return view('role.role');
    }
    /**
     * @return string
     * 角色页展示
     */
    public function getrole(){
        $res = DB::table('admin_roles')->get();
        $data['code'] = 0;
        $data['msg'] = 'success';
        $data['count'] = DB::table('admin_roles')->count();
        $data['data'] = $res;
        return json_encode($data);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     *
     */
    public function createrole(){
        return view('create.createrole');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     * 添加角色接口
     */
    public function store(Request $request)
    {
        $name = $request->input('name');
        $description = $request->input('description');;
        AdminRole::create(compact('name','description'));
        return redirect(route('admin.role'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    //获取角色的修改
    public function editrole($id){
        $roleres = DB::table('admin_roles')->where('id',$id)->first();
//        dd($res);
//        exit();
        return view('create.updaterole',compact('roleres'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     * 修改角色方法
     */
    public function updatero(Request $request){
        $cut = $request->all();
        $sql = AdminRole::where('id',$cut['id'])->update(['name'=>$cut['name'],
            'description' => $cut['description'],
            ]);

        if($sql){
            return redirect(route('admin.role'));
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 删除角色接口
     */
    public function delrole(Request $request)
    {

        $share = $request->input('id');
        $row = AdminRole::destroy($share);

        if($row){
            $res['code'] = 200;
            $res['msg'] = 'success';

        }else{
            $res['code'] = 400;
            $res['msg'] = 'fail';

        }
        return response()->json($res);
    }
    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     * 跳转分配权限
     */
    public function roleper($id){

        return view('givepromission.roper');
    }

    /**
     * @param AdminRole $role
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     * 指向分配权限
     */
    public function getsper($id)
    {
        $role = AdminRole::find($id);

        $permissions = AdminPermission::where('parent_id',0)->with('children')->get()->toArray();

        foreach($permissions as &$permission){
            if(empty($permission['children'])){
                continue;
            }
            foreach($permission['children'] as $key =>  $children){
                $sub_children = AdminPermission::where('parent_id',$children['id'])->get()->toArray();
                array_unshift($sub_children,$children);
                $permission['children'][$key] = $sub_children;
            }
        }

        $myPermissions = $role->permissions->toArray();

        $myPermissions = array_column($myPermissions, 'id');

        return view('givepromission.roper',['role'=>$role,'permissions'=>$permissions,'myPermissions'=>$myPermissions]);
    }

    /**
     * @param AdminRole $role
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     * 执行分配权限
     */
    public function showper($id) {
//        $this->validate(request(),[
//            'permissions' => 'nullable|array'
//        ]);

        $role = AdminRole::find($id);

        $permissions = AdminPermission::findMany(request('permissions'));

        $myPermissions = $role->permissions;
        $addPermissions = $permissions->diff($myPermissions);
        foreach($addPermissions as $permission){
            $role->grantPermission($permission);
        }
        $delPermissions = $myPermissions->diff($permissions);
        foreach($delPermissions as $permission){
            $role->deletePermission($permission);
        }
        return back();
    }
}